package testes;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.net.InetAddress;

import base.transporte.SRConnector;
import base.transporte.SRPacket;
import base.transporte.SRSocket;



public class TesteClienteMulti {
	
	public static void main(String[] args) throws Exception {
		long time = System.currentTimeMillis();
		InetAddress server_ip = InetAddress.getByName("localhost");
		int server_port = 60000;
		SRConnector conector = new SRConnector(server_ip, server_port, "downloader");
		ByteArrayOutputStream buffer = new ByteArrayOutputStream(15000000);
		
		
		SRSocket conector2 = conector.abrirConexao();
		SRPacket pacote = null;
		FileOutputStream fileOutStream = new FileOutputStream("C:/temp/ArquivosRecebidos/chuck.avi");
		byte[] dado;
		
		pacote = new SRPacket((short) 1, ("GET chuck.avi").getBytes());
		conector2.enviarPacote(pacote);
		
		do
		{
			dado = conector2.receberPacote().getData();
			if(!(new String(dado)).equals("EOF")){
				buffer.write(dado);
				if(buffer.size() > 10000000)
				{
					fileOutStream.write(buffer.toByteArray());	
					buffer.reset();
				}
			} else {
				fileOutStream.write(buffer.toByteArray());	
				buffer.reset();
			}
			if(System.currentTimeMillis() > time + 1000)
			{
				time = System.currentTimeMillis();
				System.out.println(conector2.getVelDownloadFormatada());
			}
		}while(!(new String(dado)).equals("EOF"));
		fileOutStream.close();
		conector.fecharConexao();
		conector2.desativar();
		
		System.out.println("Acabou!!!!!!!!!!!!!!!!!!!!!!!!!");
	}
}
